/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import alice.tuprolog.InvalidTheoryException;
import alice.tuprolog.MalformedGoalException;
import alice.tuprolog.NoSolutionException;
import alice.tuprolog.UnknownVarException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

/**
 *
 * @author Vaio
 */
public class MyMenu extends javax.swing.JFrame {

    int i = 0;
    String aux_button;
    ArrayList<JPanel> lista = new ArrayList<>();
    base_dados dados = new base_dados();
    base_dados teste12 = new base_dados();
    Passo1 panel1 = new Passo1(teste12);                 //00  
    Passo2 panel2 = new Passo2(teste12);                 //01
    Passo3 panel3 = new Passo3(teste12);                 //02
    Processor_Panel panel_process = new Processor_Panel();  //03
    Graphics_Panel panel_pvideo = new Graphics_Panel(teste12); //04
    FIM panel_fim = new FIM();          //05

    /**
     * Creates new form MyMenu
     */
    public MyMenu() throws MalformedGoalException, InvalidTheoryException, NoSolutionException, UnknownVarException {
        //base_dados teste = new base_dados();
        //System.out.println(""+ teste12.base());
        lista.add(panel1);
        lista.add(panel2);
        lista.add(panel3);
        lista.add(panel_process);
        lista.add(panel_pvideo);
        lista.add(panel_fim);

        initComponents();
        setLocationRelativeTo(null);
        lista.get(i).setSize(PanelFundo.getWidth(), PanelFundo.getHeight()); //tamanho do painel
        PanelFundo.add(lista.get(i)); //adicionando o painel       
        PanelFundo.updateUI();
        repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        NextButton = new javax.swing.JButton();
        PanelFundo = new javax.swing.JPanel();
        BackButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema Especialista PChoser");
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);

        NextButton.setText("Next ->");
        NextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextButtonActionPerformed(evt);
            }
        });

        PanelFundo.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout PanelFundoLayout = new javax.swing.GroupLayout(PanelFundo);
        PanelFundo.setLayout(PanelFundoLayout);
        PanelFundoLayout.setHorizontalGroup(
            PanelFundoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 773, Short.MAX_VALUE)
        );
        PanelFundoLayout.setVerticalGroup(
            PanelFundoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 415, Short.MAX_VALUE)
        );

        BackButton.setText("<- Back");
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BackButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(NextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(PanelFundo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(PanelFundo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NextButton)
                    .addComponent(BackButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void NextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextButtonActionPerformed
        /* Ação feita ao clicar Next onde adiciona painel 
         * a tela principal...
         */
        //System.out.println(dados.getUser());
        //painel 1
        if (panel1.getLeigo().isSelected()) {
            aux_button = "Leigo";
            //System.out.println("" + aux_button);
            //dados.setUser("Leigo");
        }
        if (panel1.getIntermediario().isSelected()) {
            aux_button = "Intermediario";
            //System.out.println("" + aux_button);
            //dados.setUser("Intermediario");
        }
        if (panel1.getAvancado().isSelected()) {
            //dados.setUser("Avancado");
        }

        //painel 1
        PanelFundo.removeAll();

        if (i < 5) {
            i++;
            lista.get(i).setSize(PanelFundo.getWidth(), PanelFundo.getHeight()); //tamanho do painel
            PanelFundo.add(lista.get(i)); //adicionando o painel
            PanelFundo.updateUI();
            repaint();
            //se leigo quando estiver na terceira posicão(tempo esperado) da lista vai direto para resultados
            if ((i == 2) && (aux_button == "Leigo")) {
                i = 4;
            }
            if ((i == 3) && (aux_button == "Intermediario")) {
                i = 4;
            }
            if (i == 5) {
                String solve = null;
                try {
                    try {
                        solve = teste12.base();
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (IOException ex) {
                        Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (MalformedGoalException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvalidTheoryException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NoSolutionException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownVarException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }

                addImagem(solve);
            }
        }
    }//GEN-LAST:event_NextButtonActionPerformed

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        PanelFundo.removeAll();
        if (i != 0) {
            i--;
            lista.get(i).setSize(PanelFundo.getWidth(), PanelFundo.getHeight()); //tamanho do painel
            PanelFundo.add(lista.get(i)); //adicionando o painel
            PanelFundo.updateUI();
            repaint();
        }
    }//GEN-LAST:event_BackButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MyMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MyMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MyMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MyMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MyMenu().setVisible(true);
                } catch (MalformedGoalException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InvalidTheoryException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NoSolutionException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnknownVarException ex) {
                    Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BackButton;
    private javax.swing.JButton NextButton;
    private javax.swing.JPanel PanelFundo;
    private javax.swing.ButtonGroup buttonGroup1;
    // End of variables declaration//GEN-END:variables

    private void addImagem(String solve) {
        solve = solve.replace("'", "");
        System.out.println("C:/Users/Vaio/Workspace/Java/IA_SE/src/Imagens/" + solve + ".jpg");
        FIM.getjLabel1().setIcon(new ImageIcon("C:/Users/Vaio/Workspace/Java/IA_SE/src/Imagens/" + solve + ".jpg"));


        FileReader file = null;
        try {
            file = new FileReader("C:/Users/Vaio/Workspace/Java/IA_SE/src/Configurações/" + solve + ".txt");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
        }
        BufferedReader br = new BufferedReader(file);
        String linha = "";
        try {
            while (br.ready()) {
                linha += br.readLine()+"\n";
            }
        } catch (IOException ex) {
            Logger.getLogger(MyMenu.class.getName()).log(Level.SEVERE, null, ex);
        }

        FIM.setText(linha);
    }
}
